<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="请输入公告标题" value=""
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-block">
                <textarea name="contend" class="layui-textarea" placeholder="请输入公告内容"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label ">有效期</label>
            <div class="layui-input-block">
                <input type="number" name="validity" placeholder="请输入公告时长" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'axios'], function () {
        var form = layui.form,
            layer = layui.layer,
            axios = layui.axios,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();


        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let obj = data.field;
            //发起异步请求
            axios({
                method: "post",
                url: 'notice/add',
                data: obj
            }).then(response => {
                //打印执行情况
                if (response.code == 0){
                    layer.msg(response.msg, function () {
                        //重载数据表格
                        layui.table.reload('currentTableId');

                        //关闭弹出层
                        layer.close(parentIndex);
                    })
                }else{
                    layer.msg(response.msg);
                }

            }).catch(error => {
                layer.msg(error);
            })

            return false;
        });

    });
</script>